package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.UserGroup;

/**
 * DAO to manage UserGroup entities.
 * 
 */
@Repository
@Transactional
public class UserGroupDAOImpl extends AbstractHibernateDAO<UserGroup, Integer> implements UserGroupDAO {

   public List<UserGroup> findByUserID(Integer userID) throws DataAccessException {
      List<UserGroup> list = findByCriteria(Restrictions.eq("userID", userID));
      return list;
   }

   public List<UserGroup> findByGroup(Integer groupID) throws DataAccessException {
      String queryString = "from UserGroup ug join fetch ug.user u where ug.groupID = ? order by ug.user.username";
      List<UserGroup> list = executeQuery(queryString, groupID);

      return list;
   }

   public UserGroup findByUserAndGroup(Integer userID, Integer groupID) throws DataAccessException {
      List<UserGroup> list = findByCriteria(Restrictions.eq("userID", userID), Restrictions.eq("groupID", groupID));
      return list.size() == 0 ? null : list.get(0);
   }
}
